package cn.iocoder.yudao.module.book.controller.admin.fine;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.book.controller.admin.fine.vo.*;
import cn.iocoder.yudao.module.book.dal.dataobject.fine.FineDO;
import cn.iocoder.yudao.module.book.service.fine.FineService;

@Tag(name = "管理后台 - 逾期罚金")
@RestController
@RequestMapping("/book/fine")
@Validated
public class FineController {

    @Resource
    private FineService fineService;

    @PostMapping("/create")
    @Operation(summary = "创建逾期罚金")
    @PreAuthorize("@ss.hasPermission('book:fine:create')")
    public CommonResult<Long> createFine(@Valid @RequestBody FineSaveReqVO createReqVO) {
        return success(fineService.createFine(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新逾期罚金")
    @PreAuthorize("@ss.hasPermission('book:fine:update')")
    public CommonResult<Boolean> updateFine(@Valid @RequestBody FineSaveReqVO updateReqVO) {
        fineService.updateFine(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除逾期罚金")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('book:fine:delete')")
    public CommonResult<Boolean> deleteFine(@RequestParam("id") Long id) {
        fineService.deleteFine(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得逾期罚金")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('book:fine:query')")
    public CommonResult<FineRespVO> getFine(@RequestParam("id") Long id) {
        FineDO fine = fineService.getFine(id);
        return success(BeanUtils.toBean(fine, FineRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得逾期罚金分页")
    @PreAuthorize("@ss.hasPermission('book:fine:query')")
    public CommonResult<PageResult<FineRespVO>> getFinePage(@Valid FinePageReqVO pageReqVO) {
        PageResult<FineDO> pageResult = fineService.getFinePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, FineRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出逾期罚金 Excel")
    @PreAuthorize("@ss.hasPermission('book:fine:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportFineExcel(@Valid FinePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<FineDO> list = fineService.getFinePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "逾期罚金.xls", "数据", FineRespVO.class,
                        BeanUtils.toBean(list, FineRespVO.class));
    }

}